package com.lml.jdk8.test06;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2020/2/29 22:08
 * Description:
 */
public class DemoTest03 {
    List<Student> list = Arrays.asList(
            new Student("lili", 19, 3160.8),
            new Student("lili2", 19, 2160.8),
            new Student("feifei", 12, 3170.8),
            new Student("lulu", 20, 4450.8),
            new Student("nana", 17, 5500),
            new Student("xiaoming", 20, 6666.6)
    );

    @Test
    void fun7() {
        //将所有学生名字做一个拼接
        String collect1 = list.stream().map(Student::getName).
                collect(Collectors.joining());
        System.out.println(collect1);
        //将所有学生名字做一个拼接,中间用 , 隔开
        String collect2 = list.stream().map(Student::getName).
                collect(Collectors.joining(","));
        System.out.println(collect2);
        //将所有学生名字做一个拼接,中间用","隔开,在用[]包起来
        String collect3 = list.stream().map(Student::getName).
                collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect3);
    }

    @Test
    void fun6() {
        //按照年龄分区  大于等于18的为一组,小于18的为另一组
        Map<Boolean, List<Student>> map = list.stream().
                collect(Collectors.partitioningBy(x -> x.getAge() >= 18));
        for (Map.Entry<Boolean, List<Student>> e : map.entrySet()) {
            System.out.println(e.getKey() + "," + e.getValue());
        }
    }

    @Test
    void fun5() {
        //按照年龄分组
        Map<Integer, List<Student>> map = list.stream().
                collect(Collectors.groupingBy(Student::getAge));
        Set<Map.Entry<Integer, List<Student>>> entries = map.entrySet();
        for (Map.Entry<Integer, List<Student>> e : entries) {
            System.out.println(e.getKey() + "," + e.getValue());
        }
    }

    @Test
    void fun4() {
        //求总人数
        Long sum = list.stream().collect(Collectors.counting());
        System.out.println(sum);
        //工资平均值
        Double avgSal = list.stream().collect(Collectors.averagingDouble(Student::getSalary));
        System.out.println(avgSal);
        //工资总和
        Double totalSal = list.stream().
                collect(Collectors.summingDouble(Student::getSalary));
        System.out.println(totalSal);
        //工资最大:
        Optional<Student> collect1 = list.stream().
                collect(Collectors.maxBy(Comparator.comparingDouble(Student::getSalary)));
        System.out.println(collect1.get());
        //工资最小
        Optional<Student> collect2 = list.stream().
                collect(Collectors.minBy(Comparator.comparingDouble(Student::getSalary)));
        System.out.println(collect2.get());

    }

    @Test
    void fun3() {
        //把所有学生的名字和薪水进行提取后放入指定的Map集合:
        Map<String, Double> collect = list.stream().
                collect(Collectors.toMap(Student::getName, Student::getSalary));
        for (Map.Entry<String, Double> e : collect.entrySet()) {
            System.out.println(e.getKey() + "," + e.getValue());
        }
    }

    @Test
    void fun2() {
        //把所有学生的名字进行提取后放入指定的Set集合:
        Set<String> collect = list.stream().
                map(Student::getName).
                collect(Collectors.toSet());
        collect.forEach(System.out::println);
    }

    @Test
    void fun1() {
        //把所有学生的名字进行提取后放入指定的List集合:
        List<String> collect = list.stream().
                map(Student::getName).
                collect(Collectors.toList());
        collect.forEach(System.out::println);
    }


}
